﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace W1
{
    public partial class BasicForm : Form
    {
        bool result = false;//判断
        DataTable dt = new DataTable();
        bool number;

        public BasicForm()
        {
            InitializeComponent();
        }

        //窗体加载事件
        private void BasicForm_Load(object sender, EventArgs e)
        {


            panelExit.Visible = true;
        }

        //调用窗体
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            //切换窗体
            Form4 form4 = new Form4();
            form4.Show();
            Hide();//隐藏窗体
        }

        //按下添加按钮方法
        private void butAdd_Click(object sender, EventArgs e)
        {
            
            //添加数据时打开编辑面板
            panelExit.Visible = false;
        }
        
        //按下取消按钮方法
        private void but_QuXiao_Click(object sender, EventArgs e)
        {
            
            //将所有输入框中的数据清零
            txtName.Text = "";
            txtNo.Text = "";
            cmbSex.Text = "";
            txtAge.Text = "";
            txtBirthday.Text = "";
            txtMinZu.Text = "";
            txtIDNumber.Text = "";
            txtJiGuan.Text = "";
            txtZZMM.Text = "";
            cmbIfDangYuan.Text = "";
            txtDetAdr.Text = "";
            cmbHuJiType.Text = "";
            txtSSH.Text = "";
            txtTel.Text = "";
            txtYZBM.Text = "";
            txtRuXueTime.Text = "";
            txtShengYuanDi.Text = "";

            panelExit.Visible = true;//关闭编辑面板
        }

        //查询方法
        private void butSelect_Click(object sender, EventArgs e)
        { 
            
            try
            {
                //非空查询
               string content = txtSelected.Text;
                if (content=="")
                {
                    MessageBox.Show("搜索框不能为空","输入提示：",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    return;
                }
                result = true;
                //学号不存在验证
                //1、sql语句  2、DBHelper调用
                if (result==true)
                {
                    string sql = string.Format("select * from BasicInfo where StudentID='{0}'", content);
                    dt = DBHelper.SelectByAdapter(sql);
                    if (dt.Rows.Count > 0)
                    {
                        ShowInfo(0);
                        ShowKongJian();
                        panelExit.Visible = false;//关闭panel容器，显示编辑信息面板
                        txtSelected.SelectAll();
                        this.txtSelected.Focus();
                        return;
                    }
                    else
                    {
                        MessageBox.Show("查无此学号！！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        panelExit.Visible = true;//打开panel容器，关闭编辑信息版面
                        txtSelected.SelectAll();
                        this.txtSelected.Focus();
                        return;
                    }
                }
                return;
                
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
        }

        //数据加载和显示功能方法
        public void ShowInfo(int i)
        {
            txtName.Text = dt.Rows[i]["StuName"].ToString();
            txtNo.Text = dt.Rows[i]["StudentID"].ToString();
            cmbSex.Text = dt.Rows[i]["Sex"].ToString();
            txtAge.Text = dt.Rows[i]["stuAge"].ToString();
            txtBirthday.Text = dt.Rows[i]["Birthday"].ToString();
            txtMinZu.Text = dt.Rows[i]["Settlement"].ToString();
            txtIDNumber.Text = dt.Rows[i]["IDNumber"].ToString();
            txtJiGuan.Text = dt.Rows[i]["Nativeplace"].ToString();
            txtZZMM.Text = dt.Rows[i]["PoliticalFace"].ToString();
            cmbIfDangYuan.Text = dt.Rows[i]["Ifmember"].ToString();
            txtDetAdr.Text = dt.Rows[i]["Detelladdress"].ToString();
            cmbHuJiType.Text = dt.Rows[i]["housetype"].ToString();
            txtSSH.Text = dt.Rows[i]["dormitoryNo"].ToString();
            txtTel.Text = dt.Rows[i]["Tel"].ToString();
            txtYZBM.Text = dt.Rows[i]["Postalcode"].ToString();
            txtRuXueTime.Text = dt.Rows[i]["Entrancetime"].ToString();
            txtShengYuanDi.Text = dt.Rows[i]["originland"].ToString();
        }

        //控件不可编辑方法
        public void ShowKongJian()
        {
            txtName.ReadOnly = true;
            txtNo.ReadOnly = true;
            cmbSex.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; 
            txtAge.ReadOnly = true;
            txtBirthday.ReadOnly = true;
            txtMinZu.ReadOnly = true;
            txtIDNumber.ReadOnly = true;
            txtJiGuan.ReadOnly = true;
            txtZZMM.ReadOnly = true;
            cmbIfDangYuan.DropDownStyle= System.Windows.Forms.ComboBoxStyle.DropDownList;
            txtDetAdr.ReadOnly = true;
            cmbHuJiType.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
            txtSSH.ReadOnly = true;
            txtTel.ReadOnly = true;
            txtYZBM.ReadOnly = true;
            txtRuXueTime.ReadOnly = true;
            txtShengYuanDi.ReadOnly = true;

        }

        //删除方法
        private void butDelete_Click(object sender, EventArgs e)
        {
            try
            {

                string content = txtSelected.Text;

                if (content == "")
                {
                    MessageBox.Show("搜索框不能为空！", "输入提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    DialogResult restlt = MessageBox.Show("确定删除该学生信息？", "删除", MessageBoxButtons.OKCancel);
                    if (restlt == DialogResult.OK)
                    {
                        string sql = string.Format("delete from BasicInfo where StudentID='{0}'", content);
                        number = DBHelper.ExecuteNonQuery(sql);

                        if (number)
                        {
                            MessageBox.Show("删除成功！！");
                            txtSelected.SelectAll();
                            this.txtSelected.Focus();

                        }
                        else
                        {
                            MessageBox.Show("删除失败！！");
                            txtSelected.SelectAll();
                            this.txtSelected.Focus();
                        }

                    }
                    panelExit.Visible = true;
                    return;

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                
            }

        }

        //修改方法
        private void butUpDate_Click(object sender, EventArgs e)
        {
            try
            {
                string content = txtSelected.Text;
                if (content == "")
                {
                    MessageBox.Show("搜索框不能为空", "输入提示：", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return;
                }
                else
                {
                    //string slq = string.Format("select * from BasicInfo where StudentID = '{0}', content");
                    //number = DBHelper.ExecuteNonQuery(slq);
                    DialogResult restlt = MessageBox.Show("确定更改该学生信息？","提示",MessageBoxButtons.OKCancel);
                    if (restlt == DialogResult.OK)
                    {

                        string sql = string.Format("update BasicInfo set StuName = '{0}', Sex = '{1}', stuAge = '{2}', IDNumber = '{3}', Birthday = '{4}', " +
                            "Settlement = '{5}', Nativeplace = '{6}', PoliticalFace = '{7}',Ifmember = '{8}', Detelladdress = '{9}', housetype = '{10}', " +
                            "dormitoryNo = '{11}', Tel = '{12}', Postalcode = '{13}', Entrancetime = '{14}', originland = '{15}' where StudentID = '{16}'", content);
                        number = DBHelper.ExecuteNonQuery(sql);
                        if (number)
                        {
                            MessageBox.Show("更改成功！！");
                            txtSelected.SelectAll();
                            this.txtSelected.Focus();

                        }
                        else
                        {
                            MessageBox.Show("更改失败！！");
                            txtSelected.SelectAll();
                            this.txtSelected.Focus();
                        }

                    }
                    panelExit.Visible = true;
                    return;

                }
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }
            
        }

        //添加按钮出现后，按下确定按钮出现的事件
        private void but_QueDing_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string stuNo = txtNo.Text.Trim();
            string sex = cmbSex.Text.Trim();
            string age = txtAge.Text.Trim();
            string brithday = txtBirthday.Text.Trim();
            //DateTime dt = new DateTime(2010, 4, 16);
            //dt.ToString();
            string minzu = txtMinZu.Text.Trim();
            string IDNumber = txtIDNumber.Text.Trim();
            string jiguan = txtJiGuan.Text.Trim();
            string zzmm = txtZZMM.Text.Trim();
            string IfDY = cmbIfDangYuan.Text.Trim();
            string address = txtDetAdr.Text.Trim();
            string type = cmbHuJiType.Text.Trim();
            string ssh = txtSSH.Text.Trim();
            string phone = txtTel.Text.Trim();
            string yzbm = txtYZBM.Text.Trim();
            string rxTime = txtRuXueTime.Text.Trim();
            string syd = txtShengYuanDi.Text.Trim();
            if (String.IsNullOrEmpty(stuNo))
            {
                MessageBox.Show("学号不能为空！");
            }
            if (String.IsNullOrEmpty(name))
            {
                MessageBox.Show("姓名不能为空！");
            }
            if (String.IsNullOrEmpty(phone))
            {
                MessageBox.Show("联系方式不能为空！");
            }
            string sql = string.Format("insert into BasicInfo(StuName,StudentID,Sex,stuAge,IDNumber,Birthday,Settlement," +
                "Nativeplace,PoliticalFace,Ifmember,Detelladdress,housetype,dormitoryNo,Tel,Postalcode,Entrancetime," +
                "originland) values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}'," +
                "'{13}','{14}','{15}','{16}')",name,stuNo,sex,age, IDNumber, brithday,minzu,jiguan,zzmm,IfDY,address,type,ssh,phone,
                yzbm,rxTime,syd);
            number = DBHelper.ExecuteNonQuery(sql);
            if (number)
            {
                MessageBox.Show("添加成功！！");
            }
            else
            {
                MessageBox.Show("添加失败！！");
            }
            panelExit.Visible = true;
        }
    }
}
